<template>
  <div>
    <el-header>
      <div class="left">
        <el-icon @click="handleCollapsed"><Menu /></el-icon>
        <span style="margin-left: 10px">高校教学考试成绩管理系统</span>
      </div>
      <div class="right">
        <span>{{ store.state.userInfo.name }}({{ store.state.userInfo.username }})———{{ roleText }}</span>
        <el-dropdown>
          <span class="el-dropdown-link">
            <el-icon :size="30" color="#fff">
              <User />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleCenter">个人中心</el-dropdown-item>
              <el-dropdown-item @click="handleLogout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
  </div>
</template>

<script setup>
import { Menu, User } from '@element-plus/icons-vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { computed } from 'vue'
const store = useStore()
const router = useRouter()
const roleText = computed(() => {
  if (store.state.userInfo.role === 1) {
    return '管理员'
  } else if (store.state.userInfo.role === 2) {
    return '教师'
  } else {
    return '学生'
  }
})
const handleCollapsed = () => {
  store.commit('changeCollapsed')
}
const handleCenter = () => {
  router.push('/center')
}
const handleLogout = () => {
  router.push('/login')
  store.commit('clearUserInfo')
  localStorage.removeItem('token')
}
</script>

<style lang="scss" scoped>
.el-header {
  background-color: #0d47a1;
  color: #fff;
  width: 100%;
  height: 60px;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.left,
.right {
  display: flex;
}
.left {
  i {
    margin: auto;
    cursor: pointer;
  }
}
.right {
  .el-dropdown {
    margin: auto;
  }
}
</style>
